<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>es6MapSet</title>
</head>
<body>
    
</body>
<script>
    /*
    //map   set
    let hm=new Map();
    //key=String
    hm.set('st01','temo');
    hm.set('st02','JS');
    hm.set('st03','Java');
    hm.set('st01','碰磕');//覆盖
    console.log(hm);
    console.log("大小="+hm.size);//3
    console.log(hm.get('st01'));//取值(碰磕)    
    //for...of
    for (const [key,val] of hm) {       //[key,val]数组解构
        console.log(key);//键
        console.log(val);//值
    }
    //for...each遍历
    hm.forEach((val,key) => {   //值,键
        console.log("key="+key+",value="+val);
    });
    //key=对象
    let hm2=new Map();
    let js={};
    let pk={name:'碰磕',age:18};
    hm2.set(js,'这是个空值');
    hm2.set(pk,'这是个pk对象');
    console.log(hm2.get(js));
    console.log(hm2.get({}));//地址不同,所以取不到
    console.log(hm2.get(pk));
    
    //Map对象的操作
    
    //把map变成二维数组
    let arr=Array.from(hm);
    console.log(arr);
    console.log(arr[0][1]);
    //合并map
    let a=new Map([['timi','提米'],['js','杰斯'],['pk','碰磕']]);
    let b=new Map([['xm','小明'],['xh','小红']]);
    let c=new Map([...a,...b]);//利用...操作符展开合并
    console.log(c);
    */


    /*
        Set 唯一值
    */
    var s1=new Set();
    s1.add("张三");
    s1.add("李四");
    s1.add("王五");
    console.log(s1);
    for (const item of s1) {
        console.log(item);  // 取值
    }
    //array转set
    let arr=['a','b','c','a'];
    let s2=new Set(arr);
    console.log(s2);//a,b,c 去重复
    //set转array
    var arr1=[...s2];
    console.log(arr1);//数组[a,b,c]
    //作用(去重、并集、交集、差集)
    //去重、并集表示唯一性
    //两个集合
    let high=new Set(['张三','李四','王五','赵六']);
    let mi=new Set(['碰磕','李四','赵六']);

    //合并
    let hb=new Set([...high,...mi]);
    console.log(hb);
    //交集
    let jiao=new Set([...high].filter(x => mi.has(x)));
    console.log(jiao);
    //差集
    let cha=new Set([...high].filter(x => !mi.has(x)));
    console.log(cha);
</script>
</html>